﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication1
{
    public partial class frmIndanhsachSV : Form
    {
        string chonnganh;
        string malop;
        bool _In;
        SqlConnection connection = new SqlConnection("Data Source=.;Initial Catalog=SM;Integrated Security=True");
        public frmIndanhsachSV()
        {
            InitializeComponent();
            DataSet ds1 = FillDataset("select nh.ManganhID, nh.Tennganh, k.Tenkhoa from  T05_Nganh_hoc nh, T16_Khoa k where k.Makhoa = nh.Makhoa");
            lkuChonNganh.Properties.DataSource = ds1.Tables[0];
        }

        DataSet FillDataset(string sql)
        {
            DataSet ds = new DataSet();
            try
            {
                SqlDataAdapter adap = new SqlDataAdapter(sql, connection);
                adap.Fill(ds);
                adap.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error" + ex.Message);
            }
            return ds;
        }

        public void get_gridview1()
        {
            DataSet ds2 = FillDataset("select lh.MalopID, lh.Tenlop from T03_Lop_hoc lh, T05_Nganh_hoc nh where lh. MaNganhID = nh.ManganhID and lh.ManganhID = N'" + chonnganh + "'");
            colMalop.FieldName = "MalopID";
            colTenlop.FieldName = "Tenlop";
            gridControl1.DataSource = ds2.Tables[0];
        }

        private void frmIndanhsachSV_Load(object sender, EventArgs e)
        {
            DataSet ds1 = FillDataset("select nh.ManganhID, nh.Tennganh, k.Tenkhoa from  T05_Nganh_hoc nh, T16_Khoa k where k.Makhoa = nh.Makhoa");
            DataRow row = ds1.Tables[0].Rows[0];
            lkuChonNganh.EditValue = row["ManganhID"].ToString();
            //chonnganh = row["ManganhID"].ToString();
            //get_gridview1();
        }

        private void lkuChonNganh_EditValueChanged(object sender, EventArgs e)
        {
            chonnganh = lkuChonNganh.EditValue.ToString();
            get_gridview1();
        }

        public void get_gridview2()
        {
            DataSet ds1 = FillDataset("select MasvID, (Holot + ' '+ Ten) as Hoten, Ngaysinh, Noisinh, Diachi from T01_Sinh_vien where MalopID = N'" + malop + "'");
            colMasv.FieldName = "MasvID";
            colHoten.FieldName = "Hoten";
            colNgaysinh.FieldName = "Ngaysinh";
            colNoisinh.FieldName = "Noisinh";
            colDiachi.FieldName = "Diachi";
            gridControl2.DataSource = ds1.Tables[0];
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            _In = true;
            malop = gridView1.GetRowCellValue(e.RowHandle, colMalop).ToString();
            get_gridview2();
        }

        private void btnIn_Click(object sender, EventArgs e)
        {
            if (_In == true)
            {
                rptDanhsachSV rpt = new rptDanhsachSV();
                rpt.DataSource = FillDataset("select MasvID, (Holot + ' '+ Ten) as Hoten, Ngaysinh, Noisinh, Diachi, MalopID from T01_Sinh_vien where MalopID = N'" + malop + "'");
                rpt.BindData();
                rpt.PrintingSystem.PreviewFormEx.MdiParent = RibbonForm1.ActiveForm;
                rpt.ShowPreview();
            }
            else
                MessageBox.Show("Xin vui lòng chọn lớp để in danh sách", "Thông báo");
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
